package com.ybkj.daijia.server.service.impl;

import com.ybkj.daijia.serverUtils.DataUtils;
import com.ybkj.daijia.common.Pager;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.page.Pageable;
import com.ybkj.daijia.page.impl.PageImpl;
import com.ybkj.daijia.page.impl.PageRequest;
import com.ybkj.daijia.server.mapper.MemberLoginRecordMapper;
import com.ybkj.daijia.server.model.MemberLogin;
import com.ybkj.daijia.server.service.MemberLoginRecordService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DefaultMemberLoginRecordService implements MemberLoginRecordService {

    @Autowired
    MemberLoginRecordMapper memberLoginRecordMapper;
    @Autowired
    DataUtils dataUtils;

    @Override
    public Map<String, Object> createLoginRecord(String phone) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            memberLoginRecordMapper.createLoginRecord(phone);
            map.put("code", 0);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 200);
            return map;
        }
    }

    @Override
    public Map<String, Object> createLoginRecord(MemberLogin memberLogin) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            memberLoginRecordMapper.insertLoginRecord(memberLogin);
            map.put("code", 0);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 200);
            return map;
        }
    }

    @Override
    public Map<String, Object> findLoginRecord(String phone) {
        Map<String, Object> map = new HashMap<String, Object>();
        MemberLogin memberLogin = memberLoginRecordMapper.findLoginRecord(phone);
        if (memberLogin == null) {
            map.put("isExist", false);
            return map;
        }
        map.put("isExist", true);
        map.put("data", memberLogin);
        return map;
    }

    @Override
    public Map<String, Object> updateLoginRecord(MemberLogin memberLogin) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            memberLoginRecordMapper.updateLoginRecord(memberLogin);
            map.put("code", 0);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 200);
            return map;
        }
    }

    @Override
    public Page<MemberLogin> findLockingMember(String param) {
        if (StringUtils.isNotBlank(param)) {
            param = "%" + param + "%";
        }
        Pageable pageable = new PageRequest(Pager.getPage(), Pager.getLimit());
        Integer page = Pager.getPage();
        Integer size = Pager.getLimit();
//		Long total = memberLoginRecordMapper.countLockingMember(param, Boolean.TRUE);
        Long total = memberLoginRecordMapper.countLockingMember(param);
//		List<MemberLogin> content = memberLoginRecordMapper.findLockingMember(param, Boolean.TRUE);
        List<MemberLogin> content = memberLoginRecordMapper.findLockingMember(param, page, size);
        return new PageImpl<MemberLogin>(content, pageable, total);

    }
}
